/*    
      ____                      _____                  +---+
     / ___\                     / __ \                 | R |
    / /                        / /_/ /                 +---+
   / /   ________  ____  ___  / ____/___  ____  __   __
  / /  / ___/ __ `/_  / / _ \/ /   / __ \/ _  \/ /  / /
 / /__/ /  / /_/ / / /_/  __/ /   / /_/ / / / / /__/ /
 \___/_/   \__,_/ /___/\___/_/    \___ /_/ /_/____  /
                                                 / /
                                            ____/ /
                                           /_____/
led.c file
编写者：小马  (Camel)
作者E-mail：375836945@qq.com
编译环境：MDK-Lite  Version: 4.23
初版时间: 2014-01-28
------------------------------------
*/

#include "Pwm.h"
#include "UART1.h"
#include "config.h"

void tim2_cfg(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  TIM_DeInit(TIM2);
  TIM_InternalClockConfig(TIM2);
  //预分频系数为72，这样计数器时钟为72MHz/72 = 1MHz
  TIM_TimeBaseStructure.TIM_Prescaler = 72;
  //设置时钟分割
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  //设置计数器模式为向上计数模式
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  //设置计数溢出大小，每计1000个数就产生一个更新事件
  TIM_TimeBaseStructure.TIM_Period = 1000-1;
  //将配置应用到TIM2中
  TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
  //禁止ARR预装载缓冲器
  TIM_ARRPreloadConfig(TIM2, DISABLE);
 
  TIM_Cmd(TIM2, ENABLE);  //使能TIMx外设
}
/****************************************************************
 * 函数名：void GPIO_Config(void) 
 * 描述  ：配置复用输出PWM时用到的I/O 
 * 输入  ：无 
 * 输出  ：无 
 * 调用  ：main()调用 
 ***************************************************************/ 
void GPIO_Config_PWM(void)  
{ 
  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* GPIOA and GPIOB clock enable */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  
 
  /*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */ 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;           // 复用推挽输出 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
} 

void pwm_cfg(void)
{
			TIM_OCInitTypeDef TimOCInitStructure;
      //设置缺省值
       TIM_OCStructInit(&TimOCInitStructure);
       //PWM模式1输出
       TimOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
       //设置占空比，占空比=(CCRx/ARR)*100%或(TIM_Pulse/TIM_Period)*100%
       TimOCInitStructure.TIM_Pulse = 400-1;
       //TIM输出比较极性高
       TimOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
       //使能输出状态
       TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
       //TIM2的CH2输出
       TIM_OC2Init(TIM2, &TimOCInitStructure);
       //设置TIM2的PWM输出为使能
       TIM_CtrlPWMOutputs(TIM2,ENABLE);
}
void gpio_cfg(void)
{
  GPIO_InitTypeDef GPIO_InitStructure; 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************** 
 * 函数名：void TIM3_Config(void)  
 * 描述  ：配置TIM3输出的PWM信号的模式 
 *      CH1:输出 T=2.5ms(f=1/2.5ms=400Hz)  D=0.6的PWM波(高电平在前，低电平在后) 
 *      CH2:输出 T=2.5ms(f=1/2.5ms=400Hz)  D=0.4的PWM波(高电平在后，低电平在前) 
 *      步骤一：通过T和TIMxCLK的时钟源确定TIM_Period和TIM_Prescaler  
 *          T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK=2.5ms  
 *          因为 TIM_Period<65535，所以 TIM_Prescaler>1,即 TIM_Prescaler=2 
 *          所以 TIM_Period=59999=0xEA5F 
 *      步骤二：根据TIM_Period的值，高低电平的先后D，确定CCR和TIM_OCPolarity 
 *          CH1：因为D=0.6，先高后低； 
 *              所以CCR1=(TIM_Period+1)* D=36000;TIM_OCPolarity=TIM_OCPolarity_High 
 *          CH2：因为D=0.4，先高后低； 
 *              所以CCR1=(TIM_Period+1)* (1-D)=36000;TIM_OCPolarity=TIM_OCPolarity_Low 
 *      步骤三：基础寄存器初始化 
 *      步骤四：通道寄存器初始化 
 *      步骤五：使能TIM3重载寄存器ARR 
 *      步骤六：使能TIM3  
 * 输入  ：无  
 * 输出  ：无  
 * 调用  ：main()调用  
 ***************************************************************/  
void TIM3_Config_PWM(void)  
{  
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
    TIM_OCInitTypeDef  TIM_OCInitStructure;  
    /* PWM信号电平跳变值 */  
    u16 CCR1= 36000;          
    u16 CCR2= 36000;  
    /*PCLK1经过2倍频后作为TIM3的时钟源等于72MHz*/  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);  
    /* Time base configuration */                                            
    TIM_TimeBaseStructure.TIM_Period =0xEA5F;  
    TIM_TimeBaseStructure.TIM_Prescaler = 2;                                    //设置预分频：预分频=2，即为72/3=24MHz  
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;                                //设置时钟分频系数：不分频  
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;                 //向上计数溢出模式  
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  
    /* PWM1 Mode configuration: Channel1 */  
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                           //配置为PWM模式1  
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                
    TIM_OCInitStructure.TIM_Pulse = CCR1;                                       //设置跳变值，当计数器计数到这个值时，电平发生跳变  
    TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;                    //当定时器计数值小于CCR1时为高电平  
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);                                    //使能通道1      
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  
    /* PWM1 Mode configuration: Channel2 */  
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
    TIM_OCInitStructure.TIM_Pulse = CCR2;                                       //设置通道2的电平跳变值，输出另外一个占空比的PWM  
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;                    //当定时器计数值小于CCR2时为低电平 
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);                                    //使能通道2  
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  
    TIM_ARRPreloadConfig(TIM3, ENABLE);                                         //使能TIM3重载寄存器ARR  
    /* TIM3 enable counter */  
    TIM_Cmd(TIM3, ENABLE);                                                      //使能TIM3   
} 
